package com.bwie.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.request.ProductRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.es.service.EsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/es")
@Log4j2
public class EsController {
    @Autowired
    private EsService service;
    @Autowired
    private HttpServletRequest requeste;

    /**
     * 搜索
     * @param productRequest
     * @return
     */
    @PostMapping("search")
    public Result<PageResult<ProductRequest>> search(@RequestBody ProductRequest productRequest){
        Result<PageResult<ProductRequest>> search = service.search(productRequest);
        log.info("请求方式：同步搜索商品信息，请求URI：{}，请求方法：{}，请求参数：{}",requeste.getRequestURI(),requeste.getMethod(), JSONObject.toJSONString(productRequest));
        Result<PageResult<ProductRequest>> result = Result.success();
        log.info("请求方式：同步搜索商品信息，请求URI：{}，请求方法：{}，回响结果：{}",requeste.getRequestURI(),requeste.getMethod(), JSONObject.toJSONString(result));
        return search;
    }


    /**
     * 批量添加
     * @param ProductRequests
     * @return
     */
    @PostMapping("insertBatch")
    public Result insertBatch(@RequestBody List<ProductRequest> ProductRequests){
        service.insertBatch(ProductRequests);
        log.info("请求方式：批量添加商品信息，请求URI：{}，请求方法：{}，请求参数：{}",requeste.getRequestURI(),requeste.getMethod(), JSONObject.toJSONString(ProductRequests));
        Result<Object> result = Result.success();
        log.info("请求方式：搜索商品信息，请求URI：{}，请求方法：{}，回响结果：{}",requeste.getRequestURI(),requeste.getMethod(), JSONObject.toJSONString(result));
        return result;
    }

    /**
     *
     * 删除全部 异步更新
     * @return
     */
    @PostMapping("deleteCourseEs")
    public Result deleteCourseEs(){
        service.deleteCourseEs();
        Result<Object> result = Result.success();
        return result;
    }
}
